﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using GameBase;
using GameLogic.Data;
using TEngine;
using UnityEngine;

namespace GameLogic.Localization
{
    public enum LanguageType
    {
        Undefined = 0,
        EnUs = 1,
        ZhCn = 2,
    }
    public class LLocalizationManager : Singleton<LLocalizationManager>
    {
        
        private LanguageType _language = LanguageType.EnUs;
        public LanguageType Language
        {
            get
            {
                return _language;
            }
            set
            {
                if (value == _language)
                {
                    return;
                }
                var oldValue = _language;
                _language = value;
                GameData.Instance.Record.Language = (int)_language;
                //GameEvent.Send(GameEventType.OnChangeLanguage, _language, oldValue);
            }
        }

        /// <summary>
        /// 获取本地化字符串
        /// </summary>
        /// <param name="key">本地化key</param>
        /// <returns>本地化字符串</returns>
        public string GetString(string key)
        {
            key = key.Replace("\r", "");
            var v = ConfigSystem.Instance.Tables.TbLocalization.GetOrDefault(key);
            if (v == null)
            {
                return key + "(未配置)";
            }
            switch (_language)
            {
                case LanguageType.EnUs:
                    return v.EnUs;
                case LanguageType.ZhCn:
                    return v.ZhCn;
            }
            return key + "(未配置)";
        }

        /// <summary> 
        /// 获取本地化字符串。和GetString的区别是会做参数填充
        /// </summary>
        /// <param name="key">本地化key</param>
        /// <param name="param">格式化输出需要的参数</param>
        /// <returns>本地化字符串</returns>
        public string GetStringEx(string key, Dictionary<string, string> param = null)
        {
            key = key.Replace("\r", "");
            var v = GetString(key);
            if (v == null)
            {
                return key +"(未配置)";
            }
            HashSet<string> keys = new HashSet<string>();
            foreach (Match match in Regex.Matches(v, @"\{([a-zA-Z0-9_]+)\}"))
            {
                keys.Add(match.Groups[1].Value);
            }
            if (keys.Count == 0)
            {
                return v;
            }

            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (var _key in keys)
            {
                string vv = null;
                if (param != null && param.TryGetValue(_key, out var value))
                {
                    vv = value;
                }
                else
                {
                    vv = FindSystemString(_key);
                }
                if (vv != null)
                {
                    dic.Add(_key, vv);
                }
            }
            
            foreach (var keyValuePair in dic)
            {
                v = v.Replace("{" + keyValuePair.Key + "}", keyValuePair.Value);
            }
            
            return v;
        }

        private string FindSystemString(string key)
        {
            switch (key)
            {
                case "g_money":
                    return "1000";
            }

            return null;
        }
    }
}